如果你在 airflow 上有很多排程,且不同排程跑得週期不同(每日、每週或每月),也同時有不同的人來維護自己寫的排程時。
那麼在查看哪些排程正常、哪些有狀況就會變得有些吃力。
這時候,除了 DAG decorator 上標註 owner 或是 tags 來分類由誰來維護、是關於什麼專案之外,還有其他可以自動通知自己排程跑失敗的方法嗎?
其實就在 DAG decorator 中的 default_args,包含排程失敗時會寄 email 給誰。
但寄件發送方要怎麼設定呢?就也是這個 SMTP 的來源怎麼設定呢?
Step 1.
在 config 資料夾下(與 docker-compose.yaml 同一層),在 airflow.cfg 檔中,設定 email 寄送的內容
[email]
email_backend = airflow.utils.email.send_email_smtp
from_email = "your_name <your_email>"
html_content_template = /opt/airflow/config/html_content_template.txt
Step 2.
設定 email 的寄送內容,也就是上面提到的 html_content_template,我分享一下我看到有人使用的模板
Try {{try_number}} out of {{max_tries + 1}}<br>
Exception:<br>{{exception_html}}<br>
Host: {{ti.hostname}}<br>
Step 3.
設定 SMTP。
可以用自己的 gmail 來進行設定,可參考這個 stackoverflow 的步驟 : https://stackoverflow.com/questions/51829200/how-to-set-up-airflow-send-email
Step 4.
在 DAG decorator 中加入如下 default_args
@DAG(
...
default_args = {
'email': ['your_email@gmail.com'],
'email_on_failure': True,
'email_on_retry': True
}
...
)
那麼後續在 DAG 出跑失敗時,就會寄送通知 email 到第4步驟中所標註的 email 地址。
參考資料 - airflow email configuration : https://airflow.apache.org/docs/apache-airflow/stable/howto/email-config.html